<template>
  <div class="music-list">
    <!--    返回按钮-->
    <div class="back" @click="back">
      <i class="icon-back"></i>
    </div>
    <!--    标题-->
    <h1 class="title" v-html="title"></h1>
    <!--    歌手背景-->
    <div class="bg-img" :style="this.bgStyle" ref="bgImg">
      <!--      随机播放全部-->
      <div class="play-wrapper" ref="playWrapper">
        <div class="play" @click="random">
          <!--          播放标志-->
          <div class="icon-play"></div>
          <span class="text">随机播放全部</span>
        </div>
      </div>
      <!--      背景虚化-->
      <div class="filter"></div>
    </div>
    <div class="bg-layer" ref="bgLayer"></div>
    <!--    歌曲列表-->
    <scroll :data="songs" class="list" @scroll="scroll"
            :listen-scroll="listenScroll" :probe-type="probeType" ref="list">
      <div class="song-list-wrapper">
        <song-list :songs="songs" @select="selectItem" :rank="rank"></song-list>
      </div>
      <div class="loading-container" v-show="!songs.length">
        <loading></loading>
      </div>
    </scroll>
  </div>
</template>

<script type="text/ecmascript-6">
  import Scroll from 'base/scroll/scroll'
  import SongList from 'base/song-list/song-list'
  import Loading from 'base/loading/loading'
  // 浏览器js兼容
  import {prefixStyle} from 'common/js/dom'
  import {mapActions} from 'vuex'
  import {mixinPlayList} from 'common/js/mixin'

  const RESERVED_HEIGHT = 40
  const transform = prefixStyle('transform')
  export default {
    mixins: [mixinPlayList],
    props: {
      // 标题
      title: {
        type: String,
        default: ''
      },
      // 背景图
      bgImg: {
        type: String,
        default: ''
      },
      // 歌曲数组
      songs: {
        type: Array,
        default: () => []
      },
      // 歌曲前面是否有排名
      rank: {
        type: Boolean,
        default: false
      }
    },
    data () {
      return {
        // 列表上滑的高度
        scrollY: 0
      }
    },
    methods: {
      // 解决mini-palyer出现时，list列表的bottom问题
      handlePlayList (playlist) {
        const bottom = playlist.length > 0 ? '60px' : ''
        this.$refs.list.$el.style.bottom = bottom
        this.$refs.list.refresh()
      },
      // 背景图片-随机播放
      random () {
        this.randomPlay({
          list: this.songs
        })
      },
      // 监测歌曲列表上滑的高度
      scroll (pos) {
        this.scrollY = pos.y
      },
      // 按钮返回
      back () {
        this.$router.back()
      },
      // 点击歌曲进入播放列表
      selectItem (item, index) {
        this.selectPlay({
          list: this.songs,
          index
        })
        // console.log(this.playlist)
      },
      ...mapActions([
        'selectPlay',
        'randomPlay'
      ])
    },
    watch: {
      // 监测歌曲列表滑动的距离，并让bg-layer层跟随移动
      scrollY (newY) {
        let translateY = Math.max(this.minTranslateY, newY)
        this.$refs.bgLayer.style[transform] = `translateY(${translateY}px)`
        let zIndex = 0
        // 背景图片放大倍数
        let scale = 0
        const percent = Math.abs(newY / this.bgImgHeight)
        // 当下滑的时候，根据滑动的newY放大背景图片
        if (newY > 0) {
          // 放大倍数
          scale = 1 + percent
          zIndex = 10
          this.$refs.bgImg.style[transform] = `scale(${scale})`
          this.$refs.bgImg.style.zIndex = zIndex
        }
        // 当滑动距离到达最顶部的时候，改变背景图片的高度及z-index
        // 好让背景图遮住歌曲列表
        if (newY < this.minTranslateY) {
          zIndex = 10
          this.$refs.bgImg.style.paddingTop = 0
          this.$refs.bgImg.style.height = `${RESERVED_HEIGHT}px`
          this.$refs.playWrapper.style.display = 'none'
        } else {
          this.$refs.bgImg.style.paddingTop = '70%'
          this.$refs.bgImg.style.height = 0
          this.$refs.playWrapper.style.display = ''
        }
        this.$refs.bgImg.style.zIndex = zIndex
      }
    },
    created () {
      this.probeType = 3
      this.listenScroll = true
    },
    computed: {
      bgStyle () {
        return `background-image:url(${this.bgImg})`
      }
    },
    mounted () {
      // 计算出背景图片的高度，那么bg-layer遮罩层最大的移动距离不能超过它
      this.bgImgHeight = this.$refs.bgImg.clientHeight
      this.minTranslateY = -this.bgImgHeight + RESERVED_HEIGHT
      this.$refs.list.$el.style.top = `${this.bgImgHeight}px`
    },
    components: {
      SongList,
      Scroll,
      Loading
    }
  }
</script>

<style scoped lang="stylus">
  @import "~common/stylus/variable.styl"
  .music-list
    position: fixed
    z-index: 100
    top: 0
    left: 0
    right: 0
    bottom: 0
    background-color: $color-background

    .back
      position: absolute
      top: 0
      left: 6px
      z-index: 50

      .icon-back
        display: block
        padding: 10px
        font-size: $font-size-large-x
        color: $color-theme

    .title
      position: absolute
      top: 0
      left: 10%
      width: 80%
      no-wrap()
      z-index: 50
      text-align: center
      line-height: 40px
      font-size: $font-size-large
      color: $color-text

    .bg-img
      position: relative
      // 宽设置父元素的100%
      // 利用padding-top:70%可实现提前占位
      // 实现宽高比70%的效果
      width: 100%
      height: 0
      padding-top: 70%
      // 默认原点为中心，现在固定原点位置为top，放大背景的时候从顶部开始
      transform-origin: top
      background-size: cover

      .play-wrapper
        position: absolute
        bottom: 20px
        z-index: 50
        width: 100%

        .play
          box-sizing: border-box
          width: 135px
          padding: 7px 0
          margin: 0 auto
          text-align: center
          border: 1px solid $color-theme
          color: $color-theme
          border-radius: 100px
          font-size: 0

          .icon-play
            display: inline-block
            vertical-align: middle
            margin-right: 6px
            font-size: $font-size-medium-x

          .text
            display: inline-block
            vertical-align: middle
            color: $color-theme
            font-size: $font-size-small

      .filter
        position: absolute
        top: 0
        left: 0
        right: 0
        bottom: 0
        background: rgba(7, 17, 27, 0.4)

    .bg-layer
      position: relative
      height: 100%
      background: $color-background

    .list
      position: fixed
      top: 0
      bottom: 0
      width: 100%
      background-color: $color-background

      .song-list-wrapper
        padding: 20px 30px

    .loading-container
      position: absolute
      width: 100%
      top: 50%
      transform: translateY(-50%)
</style>
